package com.company.huisuo.biz.activity

import android.app.Activity
import android.content.Intent
import android.widget.Toast
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.company.huisuo.base.BaseActivity
import com.company.huisuo.biz.bean.AppGameNotParticipantRespVO
import com.company.huisuo.biz.dialog.AddGroupMemberDialog
import com.company.huisuo.databinding.ActivityAddGroupBinding
import com.company.huisuo.repository.GameRepository
import com.lxj.xpopup.XPopup
import kotlinx.coroutines.launch

/**
 * 添加组合
 */
class AddGroupActivity : BaseActivity<ActivityAddGroupBinding>() {

    private val mGameRepository by lazy { GameRepository() }
    private val mNotParticipantList = mutableListOf<AppGameNotParticipantRespVO>()
    private var mGamePlayRecordId: Long = 1024L // 默认游戏记录ID，实际应该从Intent或上一页面传入
    
    private val mAddMemberDialog by lazy {
        XPopup.Builder(this).asCustom(AddGroupMemberDialog(this) {
                //添加完成
                mBinding.ivMaleGone.isVisible = true
                mBinding.tvMember.text = "王总"
            })
    }

    companion object {
        private const val EXTRA_GAME_PLAY_RECORD_ID = "game_play_record_id"
        
        fun start(activity: Activity, gamePlayRecordId: Long = 1024L) {
            val intent = Intent(activity, AddGroupActivity::class.java).apply {
                putExtra(EXTRA_GAME_PLAY_RECORD_ID, gamePlayRecordId)
            }
            activity.startActivity(intent)
        }
    }

    override fun initView() {
        // 获取游戏记录ID
        mGamePlayRecordId = intent.getLongExtra(EXTRA_GAME_PLAY_RECORD_ID, 1024L)
        
        mBinding.ivBack.setOnClickListener {
            finish()
        }
        mBinding.ivMale.setOnClickListener {
            //添加会员
            mAddMemberDialog.show()
        }
        mBinding.tvOk.setOnClickListener {
            // 获取选中的艺人
            val selectedArtist = getSelectedArtist()
            if (selectedArtist != null) {
                // 添加选中的艺人到游戏中
                addArtistToGame(selectedArtist)
            } else {
                Toast.makeText(this@AddGroupActivity, "请先选择一个艺人", Toast.LENGTH_SHORT).show()
            }
        }
        mBinding.llArtist1.setOnClickListener {
            chooseArtist(1)
        }
        mBinding.llArtist2.setOnClickListener {
            chooseArtist(2)
        }
        mBinding.llArtist3.setOnClickListener {
            chooseArtist(3)
        }
        mBinding.llArtist4.setOnClickListener {
            chooseArtist(4)
        }
        mBinding.llArtist5.setOnClickListener {
            chooseArtist(5)
        }
    }

    override fun initData() {
        // 获取未参与游戏艺人列表
        loadNotParticipantList()
    }
    
    /**
     * 获取未参与游戏艺人列表
     */
    private fun loadNotParticipantList() {
        lifecycleScope.launch {
            mGameRepository.getNotParticipantList(mGamePlayRecordId)
                .onSuccess { list ->
                    mNotParticipantList.clear()
                    mNotParticipantList.addAll(list)
                    updateArtistViews()
                }
                .onFailure { error ->
                    Toast.makeText(this@AddGroupActivity, "获取未参与艺人列表失败: ${error.message}", Toast.LENGTH_SHORT).show()
                }
        }
    }
    
    /**
     * 更新艺人视图
     */
    private fun updateArtistViews() {
        mBinding.apply {
            // 最多显示5个艺人
            val artistViews = listOf(
                Pair(llArtist1, tvArtist1),
                Pair(llArtist2, tvArtist2),
                Pair(llArtist3, tvArtist3),
                Pair(llArtist4, tvArtist4),
                Pair(llArtist5, tvArtist5)
            )
            
            // 更新艺人信息
            artistViews.forEachIndexed { index, (layout, textView) ->
                if (index < mNotParticipantList.size) {
                    val artist = mNotParticipantList[index]
                    layout.isVisible = true
                    textView.text = artist.artistName
                } else {
                    layout.isVisible = false
                }
            }
            
            // 默认选中第一个艺人
            if (mNotParticipantList.isNotEmpty()) {
                chooseArtist(1)
            }
        }
    }

    private var mSelectedArtistIndex = 0 // 当前选中的艺人索引
    
    private fun chooseArtist(pos: Int) {
        mSelectedArtistIndex = pos - 1 // 转换为0-based索引
        mBinding.apply {
            llArtist1.isSelected = pos == 1
            tvArtist1.isSelected = pos == 1
            llArtist2.isSelected = pos == 2
            tvArtist2.isSelected = pos == 2
            llArtist3.isSelected = pos == 3
            tvArtist3.isSelected = pos == 3
            llArtist4.isSelected = pos == 4
            tvArtist4.isSelected = pos == 4
            llArtist5.isSelected = pos == 5
            tvArtist5.isSelected = pos == 5
        }
    }
    
    /**
     * 获取当前选中的艺人信息
     */
    private fun getSelectedArtist(): AppGameNotParticipantRespVO? {
        return if (mSelectedArtistIndex >= 0 && mSelectedArtistIndex < mNotParticipantList.size) {
            mNotParticipantList[mSelectedArtistIndex]
        } else {
            null
        }
    }
    
    /**
     * 添加艺人到游戏中
     */
    private fun addArtistToGame(artist: AppGameNotParticipantRespVO) {
        lifecycleScope.launch {
            // 使用默认客户ID和名称，实际应用中可能需要从其他地方获取
            val customerId = 1L
            val customerName = "会员"
            
            mGameRepository.addGameParticipant(
                gamePlayRecordId = mGamePlayRecordId,
                customerId = customerId,
                customerName = customerName,
                artistId = artist.artistId,
                artistName = artist.artistName
            )
                .onSuccess {
                    Toast.makeText(this@AddGroupActivity, "添加艺人成功", Toast.LENGTH_SHORT).show()
                    finish()
                }
                .onFailure { error ->
                    Toast.makeText(this@AddGroupActivity, "添加艺人失败: ${error.message}", Toast.LENGTH_SHORT).show()
                }
        }
    }
}